//
//  WebViewController.m
//  Bespoke
//
//  Created by The Tiger Party on 12/11/6.
//
//

#import "WebViewController.h"

#define LOADINGVIEW_TAG 10000000

@implementation WebViewController

@synthesize webDisplay, Lab_webTitle, ActivityView;
@synthesize MLM_webView;

-(IBAction)goback:(id)sender
{
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(IBAction)gorefresh:(id)sender
{
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
    [webDisplay reload];
}

- (void)viewDidLoad
{ 
    NSString *urlAddress = MLM_webView.WebLink;
    
    Lab_webTitle.text = MLM_webView.MLH_html.NS_htmlTitle;
     
    webDisplay.delegate = self;
	[self.view addSubview: webDisplay];
	[webDisplay loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: urlAddress]]];
    
    [super viewDidLoad];
    
    [NSThread detachNewThreadSelector:@selector(threadStartAnimating) toTarget:self withObject:nil];
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
	
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [NSThread detachNewThreadSelector:@selector(threadStopAnimating) toTarget:self withObject:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UIActivityIndicatorView

-(void)threadStartAnimating {
    UIView *Vw_Loading = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
    [Vw_Loading setTag:LOADINGVIEW_TAG];
    [Vw_Loading setBackgroundColor:[UIColor blackColor]];
    [Vw_Loading setAlpha:0.5];
    [self.view addSubview:Vw_Loading];
    [self.view bringSubviewToFront:Vw_Loading];
    [self.view bringSubviewToFront:ActivityView];
    ActivityView.hidden = NO;
    [ActivityView startAnimating];
}

-(void)threadStopAnimating {
    ActivityView.hidden = YES;
    [ActivityView stopAnimating];
    
    [[NSOperationQueue mainQueue] addOperationWithBlock:
     ^(void) {
         [[self.view viewWithTag:LOADINGVIEW_TAG] removeFromSuperview];
     }];
}

#pragma mark - Interface Orientation Management

//For iOS 5
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
	return ((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation == UIInterfaceOrientationLandscapeRight));
}

//For iOS 6
-(BOOL)shouldAutorotate {
    return YES;
}

-(NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskLandscape;
}

@end
